﻿/**
 * SlotDuino
 * 
 * Copyright (c) 2011/2012 Thiago Hummel Moreira, Ricardo Mizuuti, Rafael Mufato, Bruno Sakai, Andre Turina
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using SlotDuino.DAL;

namespace SlotDuino
{
	/// <summary>
	/// Interaction logic for Header.xaml
	/// </summary>
	public partial class Header : UserControl
	{
        private Window _parentWindow;

        public Window ParentWindow
        {
            get {
                if (null == _parentWindow)
                {
                    _parentWindow = Window.GetWindow(this);
                }

                return _parentWindow;
            }
        }

        public Operator LoggedUser
        {
            get { return ((App)Application.Current).LoggedUser; }
        }

        public NavigationService Navigation
        {
            get { return (NavigationService)Application.Current.Properties["navigationService"]; }
        }

		public Header()
		{
			this.InitializeComponent();
		}

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //Avoid problems with blend/VS desing view
            if (DesignerProperties.GetIsInDesignMode(this)) return;

            //Attach a listener to the application event on logged user changes
            App slotduino = (App)Application.Current;
            slotduino.LoggedUserChanged += new System.EventHandler(slotduino_LoggedUserChanged);
            
            //Update the displayed data
            UpdateLoggedUserData();
        }

        void slotduino_LoggedUserChanged(object sender, System.EventArgs e)
        {
            UpdateLoggedUserData();
        }

        private void UpdateLoggedUserData()
        {
            if (null != LoggedUser)
            {
                userInfo.Text = "Usuário: " + LoggedUser.Name + "\nTipo: " + (LoggedUser.IsAdministrator ? "Administrador" : "Operador");
                userInfo.Visibility = System.Windows.Visibility.Visible;
                userInfo.UpdateLayout();

                btnBack.Margin = new Thickness(0, 0, userInfo.ActualWidth + 20, 8);
                btnBack.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                userInfo.Visibility = System.Windows.Visibility.Collapsed;
                btnBack.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            if (Navigation.CanGoBack) Navigation.GoBack();
        }

        #region Window controls
        public void DragWindow(object sender, MouseButtonEventArgs args)
        {
            ParentWindow.DragMove();
        }

        private void CloseWindow(object sender, MouseButtonEventArgs e)
        {
            ParentWindow.Close();
        }

        private void MinimizeWindow(object sender, MouseButtonEventArgs e)
        {
            ParentWindow.WindowState = WindowState.Minimized;
        }

        private void MaximizeWindow(object sender, MouseButtonEventArgs e)
        {
            ParentWindow.SizeToContent = (ParentWindow.WindowState == WindowState.Maximized ? SizeToContent.WidthAndHeight : SizeToContent.Manual);
            ParentWindow.WindowState = (ParentWindow.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized);
        }
        #endregion
    }
}